package freecomm.study.listview2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity
{

    private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    private MyAdapter adapter;
    private ListView mainListView;

    protected final class ItemViewHolder
    {
        protected ImageView iconIM;
        protected TextView titleTV;
    }

    protected class MyAdapter extends BaseAdapter
    { 
        private LayoutInflater mInflater;
        private int selectItem = -1;

        public MyAdapter(Context context)
        {
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount()
        {
            // TODO Auto-generated method stub

            return list.size();
        }

        @Override
        public Object getItem(int position)
        {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            // TODO Auto-generated method stub
            Log.i("shit", "=null");
            ItemViewHolder holder = null;
            if (convertView == null)
            {
                Log.i("shit", "if");
                holder = new ItemViewHolder();
                convertView = mInflater.inflate(R.layout.list_item, null);
                holder.iconIM = (ImageView) convertView.findViewById(R.id.listItemImage);
                holder.titleTV = (TextView) convertView.findViewById(R.id.listItemText);
                convertView.setTag(holder);
            } else
            {
                holder = (ItemViewHolder) convertView.getTag();
            }
            Log.i("shit", "before im" + list.get(position).get("img"));
            holder.iconIM.setImageResource(((Integer) list.get(position).get("img")).intValue());

            // holder.iconIM.setImageDrawable(getResources().getDrawable(R.drawable.ic_camera_normal));
            Log.i("shit", "after im");
            holder.titleTV.setText((String) list.get(position).get("text"));

            if (position == selectItem)
            {

            //    convertView.setBackgroundColor(Color.GRAY);
            } else
            {
        //        convertView.setBackgroundColor(Color.WHITE);
            }
            return convertView;
        }

        public void setSelectItem(int selectItem)
        {
            this.selectItem = selectItem;
        }

    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.list_view);

        getData();

        mainListView = (ListView) findViewById(R.id.main_list_view);

        adapter = new MyAdapter(this);
        mainListView.setAdapter(adapter);
        mainListView.setOnItemSelectedListener(myItemSelectedListener);
        mainListView.setOnItemClickListener(myItemClickListener);

    }

    AdapterView.OnItemClickListener myItemClickListener = new OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            // TODO Auto-generated method stub
            adapter.setSelectItem(arg2);
            adapter.notifyDataSetInvalidated();
        }
    };

    AdapterView.OnItemSelectedListener myItemSelectedListener = new OnItemSelectedListener()
    {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            // TODO Auto-generated method stub
            adapter.setSelectItem(arg2);
            adapter.notifyDataSetInvalidated();

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {
            // TODO Auto-generated method stub
            adapter.setSelectItem(-1);
            adapter.notifyDataSetInvalidated();
        }
    };

    private List<Map<String, Object>> getData()
    {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("img", R.drawable.camera_selector);
        map.put("text", "Camera");
        list.add(map);
        map = new HashMap<String, Object>();
        map.put("img", R.drawable.download_selector);
        map.put("text", "Download");
        list.add(map);
        map = new HashMap<String, Object>();
        map.put("img", R.drawable.music_selector);
        map.put("text", "Music");
        list.add(map);
        map = new HashMap<String, Object>();
        map.put("img", R.drawable.photo_selector);
        map.put("text", "Photo");
        list.add(map);
        map = new HashMap<String, Object>();
        map.put("img", R.drawable.video_selector);
        map.put("text", "Video");
        list.add(map);
        return list;
    };
}